package com.lordtan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lordtan.persistance.dao.IcommonDao;
import com.lordtan.persistance.entity.User;
import com.lordtan.service.ILogInService;

/**
 * 登陆服务
 * @author lordtan
 * @date 2013-1-2
 */
@Service
public class LogInService implements ILogInService {
    
	@Autowired
	private IcommonDao commonDao;
	
	/* 
	 * 检查登陆信息是否正确
	 * @see org.lordtan.service.ILogInService#checkLogin(java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean checkLogin(String account, String password) {
		String jpql = "select u from User u where u.email=:account";
		Map<String,Object> parameter = new HashMap<String, Object>(); //查询参数
		parameter.put("account", account);
		List<User> relList = commonDao.queryByJpql(jpql, parameter);
		if(null==relList || relList.size()<1){
			return false;
		}
		String pwd = relList.get(0).getPassword();
		if(password.equals(pwd)){
			return true;
		}
		return false;
	}

	/* 
     * 更好用户密码
	 * @see org.lordtan.service.ILogInService#changePassWord(java.lang.String, java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String changePassWord(String account, String oldPwd, String newPwd) {
		String jpql = "select u from User u where u.account=:account";
		Map<String,Object> parameter = new HashMap<String, Object>(); //查询参数
		parameter.put("account", account);
		List<User> relList = commonDao.queryByJpql(jpql, parameter);
		if(null==relList || relList.size()<1){
			return "-1";  //账号不存在
		}
		String pwd = relList.get(0).getPassword();
		if(!oldPwd.equals(pwd)){
			return "-2"; //原密码错误
		}
		
		return "1";
	}

}
